package application.generateReports;

import java.sql.SQLException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import application.CommunicationModule.Communication;

public class SingleReport extends Report {

	private DataToReport dataToReport = new DataToReport();

	
	public SingleReport(String user, String password, int id) throws SQLException, ClassNotFoundException {
		super();
		this.user = user;
		this.password = password;
		this.creationDate = new  Date(calendar.get(Calendar.YEAR) - 1900,
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
        
		this.creationTime = new Time(calendar.get(Calendar.HOUR_OF_DAY),
				calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
		
		
	
		this.reportName = "Report for single client";
		
		Communication c = new Communication(this.user, this.password);
		this.dataToReport.setC((c.getClientsByField("id", String.valueOf(id)).get(0)));
		this.dataToReport.setParcelsPosted(c.getParcelsByClient(id).size());	
		this.dataToReport.setParcelsReceived(c.getParcelsByRecipent(id).size());
		
		
		String delemiter = "-";
		String[] temp1;
		String[] temp2;

		this.dataToReport.setLastparcelsPosted(c.getParcelsByClient(id).get(0).getDate());
		temp2 = dataToReport.getLastparcelsPosted().split(delemiter);
		for(int i=0 ; i < c.getParcelsByClient(id).size(); i++) {
			temp1 = c.getParcelsByClient(id).get(i).getDate().split(delemiter);

			if(Integer.parseInt(temp1[0]) > Integer.parseInt(temp2[0])){
				this.dataToReport.setLastparcelsPosted(c.getParcelsByClient(id).get(i).getDate());
				temp2 = dataToReport.getLastparcelsPosted().split(delemiter);
			}
			else if(Integer.parseInt(temp1[0]) == Integer.parseInt(temp2[0])){
				if(Integer.parseInt(temp1[1]) > Integer.parseInt(temp2[1])){
				this.dataToReport.setLastparcelsPosted(c.getParcelsByClient(id).get(i).getDate());
				temp2 = dataToReport.getLastparcelsPosted().split(delemiter);}
				else if(Integer.parseInt(temp1[1]) == Integer.parseInt(temp2[1])){
					if(Integer.parseInt(temp1[2]) > Integer.parseInt(temp2[2])){
						this.dataToReport.setLastparcelsPosted(c.getParcelsByClient(id).get(i).getDate());
						temp2 = dataToReport.getLastparcelsPosted().split(delemiter);
					}
				}
			}

			
			this.dataToReport.setParcelsCost(c.getParcelsByClient(id).get(i).getPrice() + this.dataToReport.getParcelsCost());	
			
		}
		
			
	}



	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}








	public String getPassword() {
		return password;
	}








	public void setPassword(String password) {
		this.password = password;
	}



	public DataToReport getDataToReport() {
		return dataToReport;
	}








	public void setDataToReport(DataToReport dataToReport) {
		this.dataToReport = dataToReport;
	}








	public Date getCreationDate() {
		return creationDate;
	}








	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}








	public Time getCreationTime() {
		return creationTime;
	}








	public void setCreationTime(Time creationTime) {
		this.creationTime = creationTime;
	}








	public String getReportName() {
		return reportName;
	}








	public void setReportName(String reportName) {
		this.reportName = reportName;
	}








	public SimpleDateFormat getF() {
		return format;
	}








	public void setF(SimpleDateFormat f) {
		this.format = f;
	}








	public Calendar getCalendar() {
		return calendar;
	}








	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}


	

	
}




